﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cofe.Core.Property;
using Cofe.Core.Validater;

namespace Cofe.Core.Utils
{
    public static class PropertyUtils
    {
        public static object FindRelatedProperty(object property)
        {
            RelatedActionAttribute rat = AttributeUtils<RelatedActionAttribute>.FindAttribute(property);
            if (rat != null)
                return rat.Property;
            return null;
        }

        public static PropertyRetrivalOptions GetPropertyRetrivalOptions(object property)
        {
            InvokeBehaviorAttribute act = CofeServices.PropertyDictionary.AttributeHelper.FindAttribute<InvokeBehaviorAttribute>(property);
            //InvokeBehaviorAttribute act = AttributeUtils<InvokeBehaviorAttribute>.FindAttribute(property);
            var retVal = act is AggregateResultAttribute ? PropertyRetrivalOptions.Aggregate
              : act is HandleOnesAttribute ? PropertyRetrivalOptions.HandleOnes
              : act is ReturnMaxOnlyAttribute ? PropertyRetrivalOptions.Max
              : act is ReturnMinOnlyAttribute ? PropertyRetrivalOptions.Min
              : PropertyRetrivalOptions.First;

            if (property.Equals(CacheProperties.Id))
                return PropertyRetrivalOptions.First;

            if (!(retVal == PropertyRetrivalOptions.Max || retVal == PropertyRetrivalOptions.Min)
                && property is CacheProperties)
                return PropertyRetrivalOptions.CacheOnly;

            return retVal;
        }

        public static IEnumerable<object> FilterProperty(IEnumerable<object> properties, MatchMode matchMode = MatchMode.All,
         params IPropertyFilter[] filters)
        {
            return (from p in properties
                    where FilterUtils.MatchPropertyFilters(p, matchMode, filters)
                    select p).ToArray();
        }

        public static string PropertiesToCsv(object[] properties)
        {
            return String.Join(",",
                    (from prop in properties select (int)prop).ToArray());
        }

        public static object[] CsvToProperties(string propStr)
        {
            List<object> retVal = new List<object>();
            if (!(String.IsNullOrEmpty(propStr)))
                foreach (var pStr in propStr.Split(','))
                {
                    var prop = CofeServices.PropertyDictionary.GetProperty(StringUtils.TryParseInt(pStr, -1));
                    if (prop != null)
                        retVal.Add(prop);
                }
            return retVal.ToArray();
        }

        public static string PropertyDefinitionsToCsv(IPropertyDefinitions[] propertiesDefinitions)
        {
            return String.Join(",",
                    (from pd in propertiesDefinitions select pd.BeginId).ToArray());
        }

        public static IPropertyDefinitions[] CsvToPropertyDefinitions(string propDefStr)
        {
            List<IPropertyDefinitions> retVal = new List<IPropertyDefinitions>();
            if (!(String.IsNullOrEmpty(propDefStr)))
                foreach (var pdefStr in propDefStr.Split(','))
                {
                    var def = CofeServices.PropertyDictionary.PropertyDefinitions.FindDefinition(StringUtils.TryParseInt(pdefStr, -1));
                    if (def != null)
                        retVal.Add(def);
                }
            return retVal.ToArray();
        }

    }
}
